﻿using Koala.Pro.CodeGenerator.UI.DatabaseModel.Schema;

namespace Koala.Pro.CodeGenerator.UI.Common.Adapter.SqlsugarWPF
{
    public class EditFormAdapter : AbstractAdapter
    {
        private const string SUFFIX_OF_PO_CLASS = "EditPage";

        private string _tableName = string.Empty;

        private List<string> _editFields;

        private List<string> _editCheck;

        private List<string> _searchFields;

        private List<string> _searchDisplay;

        public EditFormAdapter(DatabaseInfo databaseInfo, List<string> selectedTableNames, List<string> searchFields, List<string> searchDisplay, List<string> editFields, List<string> editCheck, string duplicateCheckField, string templateFile)
            : base(databaseInfo, selectedTableNames, templateFile)
        {
            DirectoryOfOutput = "Views/Pages/";

            if (templateFile.Contains(SUFFIX_OF_PO_CLASS + ".xaml.cs"))
            {
                FileExtension = ".xaml.cs";
            }
            else
            {
                FileExtension = ".xaml";
            }

            _tableName = selectedTableNames.FirstOrDefault();

            _searchFields = searchFields;
            _searchDisplay = searchDisplay;
            _editFields = editFields;
            _editCheck = editCheck;

            var tableInfo = databaseInfo.TableList[_tableName];

            if (templateFile.Contains(SUFFIX_OF_PO_CLASS))
            {
                FileNameOfOutput = tableInfo.Name.Alias.ToCapit() + SUFFIX_OF_PO_CLASS;
            }
            else
            {
                FileNameOfOutput = tableInfo.Name.Alias.ToCapit() + SUFFIX_OF_PO_CLASS;
            }

            context.Put("DisplayColumnString", string.Join(",", _searchDisplay));

            PrepareClass(tableInfo);
            OutputFile();
        }

        private void PrepareClass(TableInfo tableInfo)
        {
            context.Put("TableInfo", tableInfo);
            context.Put("ClassName", tableInfo.Name.Alias.ToCapit());
            string primaryKeyNetType = "int";
            foreach (var item in tableInfo.ColumnList)
            {
                if (item.Value.IsPrimaryKey)
                {
                    primaryKeyNetType = item.Value.NetType.Alias.ToString();
                    break;
                }
            }

            var columnEdit = new List<ColumnInfo>();
            foreach (var item in _editFields)
            {
                if (tableInfo.ColumnList.ContainsKey(item))
                {
                    var columnInfo = tableInfo.ColumnList[item];
                    columnEdit.Add(columnInfo);
                }
            }
            if (columnEdit.Count > 0)
            {
                context.Put("ColumnEdit", columnEdit);
            }

            var columnCheck = new List<ColumnInfo>();
            foreach (var item in _editCheck)
            {
                if (tableInfo.ColumnList.ContainsKey(item))
                {
                    var columnInfo = tableInfo.ColumnList[item];
                    columnCheck.Add(columnInfo);
                }
            }
            if (columnCheck.Count > 0)
            {
                context.Put("ColumnCheck", columnCheck);
            }

            context.Put("PrimaryKeyNetType", primaryKeyNetType);
        }
    }
}
